<template>
  <el-dialog
    :title="!dataForm.eventId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动标题" prop="eventTitle">
      <el-input v-model="dataForm.eventTitle" placeholder="活动标题"></el-input>
    </el-form-item>
    <el-form-item label="活动对应的商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="活动对应的商品id"></el-input>
    </el-form-item>
    <el-form-item label="活动的正文" prop="eventContent">
      <el-input v-model="dataForm.eventContent" placeholder="活动的正文"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="类型（1.活动  2.品牌新闻）" prop="eventType">
      <el-input v-model="dataForm.eventType" placeholder="类型（1.活动  2.品牌新闻）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          eventId: 0,
          eventTitle: '',
          productId: '',
          eventContent: '',
          modifyTime: '',
          eventType: '',
          deleted: ''
        },
        dataRule: {
          eventTitle: [
            { required: true, message: '活动标题不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '活动对应的商品id不能为空', trigger: 'blur' }
          ],
          eventContent: [
            { required: true, message: '活动的正文不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          eventType: [
            { required: true, message: '类型（1.活动  2.品牌新闻）不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.eventId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.eventId) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/event/info/${this.dataForm.eventId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.eventTitle = data.event.eventTitle
                this.dataForm.productId = data.event.productId
                this.dataForm.eventContent = data.event.eventContent
                this.dataForm.modifyTime = data.event.modifyTime
                this.dataForm.eventType = data.event.eventType
                this.dataForm.deleted = data.event.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/event/${!this.dataForm.eventId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'eventId': this.dataForm.eventId || undefined,
                'eventTitle': this.dataForm.eventTitle,
                'productId': this.dataForm.productId,
                'eventContent': this.dataForm.eventContent,
                'modifyTime': this.dataForm.modifyTime,
                'eventType': this.dataForm.eventType,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
